Page({
  data: {
    addresses: [
      { id: 1, name: '张三', detail: '上海市浦东新区xxx路xxx号', selected: false },
      { id: 2, name: '李四', detail: '北京市朝阳区xxx街xxx号', selected: true },
      { id: 3, name: '冯小林', detail: '四川省巴中市达州', selected: false },
      { id: 4, name: '苏琪杨', detail: '四川省资阳市安岳县', selected: false },
      // 这里可以添加更多默认地址
    ],
  },
  selectAddress: function(e) {
    const index = e.currentTarget.dataset.index;
    const addresses = this.data.addresses.map((address, idx) => ({
      ...address,
      selected: idx === index,
    }));
    this.setData({ addresses });
  },
  deleteAddress: function(e) {
    const id = e.currentTarget.dataset.id;
    const addresses = this.data.addresses.filter(address => address.id !== id);
    this.setData({ addresses });
  },
  addAddress: function() {
    // 跳转到新增地址的页面或逻辑
    wx.navigateTo({ url: '/pages/xinzeng/xinzeng' });
    console.log('添加地址');
  },
  onShow: function() {
    // 获取全局数据
    const app = getApp();
    // 从全局变量中获取新地址信息
    const newAddress = app.globalData.newAddress;
    if (newAddress) {
      const addresses = this.data.addresses;
      addresses.push(newAddress);
      this.setData({ addresses });
      
      // 清空全局变量中的新地址信息
      app.globalData.newAddress = null;
    }
  },
})